<template>
  <div>
    <el-row class="title-bar">
      <span class="title-bar-span">工作人员</span>
      <el-button class="title-button" icon="el-icon-plus" type="text"  v-hasPermi="['labdata:employee:edit']" @click="handleAdd"></el-button>
    </el-row>
    <el-table v-loading="loading" :data="employeeList">
      <el-table-column label="工号" align="center" prop="employeeNo" width="80"/>
      <el-table-column label="姓名"  prop="employeeName" />
      <el-table-column label="手机号" align="center" prop="phonenumber" width="100"/>
      <el-table-column label="出生月份"  prop="birthday" width="60"/>
      <el-table-column label="文化程度" align="center" prop="education" width="80"/>
      <el-table-column label="所学专业" align="center" prop="majorStudied" width="100"/>
      <el-table-column label="任职日期" align="center" prop="hireStartDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hireStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任职状态" align="center" prop="hireStatusOpt" :formatter="hireStatusOptFormat" width="60"/>
      <el-table-column label="任职编号 " align="center" prop="approvalNo" width="60"/>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"  v-hasPermi="['labdata:employee:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['labdata:employee:edit']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改实验室工作人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="720px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工号" prop="employeeNo">
              <el-input v-model="form.employeeNo" placeholder="请输入工号" size="small" class="input_w"  @blur="searchUser"
                  @keyup.enter.native="searchUser"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="employeeName">
              <el-input v-model="form.employeeName" placeholder="请输入姓名" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门" prop="orgName">
              <el-input v-model="currentOrg.orgName" disabled size="small" class="input_w"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任职类型" prop="hireTypeOpt">
              <el-select v-model="form.hireTypeOpt" placeholder="请选择任职类型" size="small" class="input_w">
                <el-option v-for="dict in hireTypeOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="角色" prop="roleIds">
              <el-select v-model="form.roleIds" multiple placeholder="请选择" size="small" @change="onInput()" class="input_w">
                <el-option v-for="item in roleOptions" :key="item.roleId" :label="item.roleName" :value="item.roleId"
                  :disabled="item.status == 1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="form.sex" placeholder="请选择" size="small" class="input_w">
                <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任职编号 " prop="approvalNo">
              <el-input v-model="form.approvalNo" placeholder="请输入任职编号 " size="small" class="input_w"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技术职务" prop="title">
              <el-input v-model="form.title" placeholder="请输入技术职务 " size="small" class="input_w"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="出生月份" prop="birthday">
              <el-input v-model="form.birthday" placeholder="请输入出生月份" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文化程度" prop="education">
              <el-input v-model="form.education" placeholder="请输入文化程度" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所学专业" prop="majorStudied">
              <el-input v-model="form.majorStudied" placeholder="请输入所学专业" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专家类别" prop="expertAspect">
              <el-input v-model="form.expertAspect" placeholder="请输入专家类别" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
        </el-row>
          <el-row>
            <el-col :span="12">
            <el-form-item label="学历教育时间" prop="academicEducation">
              <el-input v-model="form.academicEducation" placeholder="请输入学历教育时间(天)" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="非学历教育时间" prop="nonDegreeEducation">
              <el-input v-model="form.nonDegreeEducation" placeholder="请输入非学历教育时间(天)" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="国外学历教育" prop="outerAcademicEdu">
              <el-input v-model="form.outerAcademicEdu" placeholder="请输入国外学历教育时间(天)" size="small" class="input_w"/>
          </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="国外非学历教育" prop="outerNonDegreeEdu">
              <el-input v-model="form.outerNonDegreeEdu" placeholder="请输入国外非学历教育时间(天)" size="small" class="input_w"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任职日期" prop="hireStartDate">
              <el-date-picker clearable size="small" class="input_w" v-model="form.hireStartDate" type="date"
                value-format="yyyy-MM-dd" placeholder="选择任职日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离职日期" prop="hireEndDate" v-if="form.employeeId>0" class="input_w">
              <el-date-picker clearable size="small" style="max-width: 200px;"
                v-model="form.hireEndDate" type="date" value-format="yyyy-MM-dd" placeholder="选择离职日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" size="small" style="width: 540px;"/>
        </el-form-item>
        <el-form-item label="任职文件" >
          <UploadCom ref="upload" @submitData="submitData"  description="" :userFileList="fileList" class="form_long"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"  v-hasPermi="['labdata:employee:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listEmployee,
    getEmployee,
    delEmployee,
    addEmployee,
    updateEmployee,
    exportEmployee,
    searchEmployee
  } from "@/api/labdata/employee";
  import UploadCom from "@/views/labdata/commons/upload.vue";
  export default {
    name: "EmployeeCom",
    components:{
      UploadCom
    },
    data() {
      return {
        //附件集合
        fileList: [],
        currentOrg: {},
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 实验室工作人员表格数据
        employeeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 任职类型字典
        hireTypeOptOptions: [],
        // 任职状态字典
        hireStatusOptOptions: [],
        // 状态数据字典
        statusOptions: [],
        // 性别状态字典
        sexOptions: [],
        // 角色选项
        roleOptions: [],
        initPassword: undefined,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orgId: null,
        },
        // 表单参数
        form: {
          roleIds:[],
        },
        // 表单校验
        rules: {
         /* hireTypeOpt: [{
            required: true,
            message: "任职类型不能为空",
            trigger: "change"
          }], */
          employeeNo: [{
            required: true,
            message: "工号不能为空",
            trigger: "blur"
          }],
          employeeName: [{
            required: true,
            message: "姓名不能为空",
            trigger: "blur"
          }],
         /* hireStartDate: [{
            required: true,
            message: "任职起始日期不能为空",
            trigger: "blur"
          }], */
          approvalNo: [{
            required: true,
            message: "任职批文号不能为空",
            trigger: "blur"
          }],
          roleIds :[
            {
              required: true,
              message: "角色不能为空",
              trigger: ["blur", "change"]
            }
          ],
          title: [{
            required: true,
            message: "职称不能为空",
            trigger: "blur"
          }],
          email: [{
              required: true,
              message: "邮箱地址不能为空",
              trigger: "blur"
            },
            {
              type: "email",
              message: "'请输入正确的邮箱地址",
              trigger: ["blur", "change"]
            }
          ],
          phonenumber: [/*{
              required: true,
              message: "手机号码不能为空",
              trigger: "blur"
            },*/
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ]
        }
      };
    },
    props: {
      childOrg: {
        type: Object,
        default: () => ({})
      },
    },
    watch: {
      childOrg: function(newVal, oldVal) {
        this.currentOrg = newVal;
        this.getList();
      }
    },
    created() {
      this.getDicts("hireTypeOpt").then(response => {
        this.hireTypeOptOptions = response.data;
      });
      this.getDicts("hireStatusOpt").then(response => {
        this.hireStatusOptOptions = response.data;
      });
      this.getDicts("sys_user_sex").then(response => {
        this.sexOptions = response.data;
      });
      this.getConfigKey("sys.user.initPassword").then(response => {
        this.initPassword = response.msg;
      });
      this.currentOrg = this.childOrg;
      this.getList();
    },
    methods: {
      /** 查询实验室工作人员列表 */
      getList() {
        this.loading = true;
        this.queryParams.orgId = this.currentOrg.orgId;
        //console.log("orgId="+this.queryParams.orgId);
        listEmployee(this.queryParams).then(response => {
          this.employeeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      searchUser() {
        if (!this.form.employeeNo) {
          return;
        } else
          searchEmployee(this.form.employeeNo).then(response => {
            let ret = response.data;
            if(this.currentOrg.orgId==ret.orgId){
              this.msgError('改员工号已经添加过');
              this.form.employeeNo = null;

            }else{
              this.form.employeeName = ret.employeeName;
              this.form.email = ret.email;
              this.form.phonenumber = ret.phonenumber;
              this.form.title = ret.title;
              this.form.sex = ret.sex;
              this.form.userId = ret.userId;
              this.form.deptId = ret.deptId;
              //console.log("form info:"+JSON.stringify(response.data))
              //this.form = ret;
             this.$forceUpdate();
           }
          });

      },
      onInput() {
        this.$forceUpdate();
      },
      // 任职类型字典翻译
      hireTypeOptFormat(row, column) {
        return this.selectDictLabel(this.hireTypeOptOptions, row.hireTypeOpt);
      },
      // 任职状态字典翻译
      hireStatusOptFormat(row, column) {
        return this.selectDictLabel(this.hireStatusOptOptions, row.hireStatusOpt);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          roleIds:[],
        };
        this.fileList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.employeeId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        if(!this.currentOrg.orgFunctionOpt){
          this.currentOrg.orgFunctionOpt = 3;
          this.title = "添加平台工作人员";
        }else{
          this.title = "添加实验室工作人员";
        }
        getEmployee(this.currentOrg.orgFunctionOpt).then(response => {
          this.roleOptions = response.roles;
          this.open = true;
          this.form.password = this.initPassword;
        });

      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const employeeId = row.employeeId || this.ids;
        if(!this.currentOrg.orgFunctionOpt){
          this.currentOrg.orgFunctionOpt = 3;
          this.title = "修改平台工作人员";
        }else{
          this.title = "修改实验室工作人员";
        }
        getEmployee(this.currentOrg.orgFunctionOpt, employeeId).then(response => {
          this.form = response.data;
          this.roleOptions = response.roles;
          this.fileList = this.form.images;
          this.open = true;
        });
      },
      /** 提交按钮 */
      submitForm(){
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$refs["upload"].doSubmit();
          }
        });
      },
      submitData(fileList) {
        this.form.images = fileList;
        this.form.orgId = this.currentOrg.orgId;
        if (this.form.employeeId != null) {
          updateEmployee(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          addEmployee(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const employeeIds = row.employeeId || this.ids;
        this.$confirm('是否确认删除实验室工作人员工号为"' + row.employeeNo + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEmployee(employeeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
    }
  };
</script>
<style type="text/css" scoped lang="scss">
  .input_w{
    width: 200px;
  }
</style>
